/*
 * @Author: gonghairun
 * @Date: 2022-05-12 21:12:05
 * @LastEditors: gonghairun
 * @LastEditTime: 2022-05-20 16:22:35
 * @Description:
 */

import Vue from "vue"
import VueRouter from "vue-router"
import { routers } from "./router"
import { ExternalUrl } from "../network/http"
import config from "../network/config.js"

Vue.use(VueRouter)

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

// 路由配置
const RouterConfig = {
	// mode: 'history',
	routes: routers,
}

export const router = new VueRouter(RouterConfig)

router.beforeEach((to, from, next) => {
	// iView.LoadingBar.start();
	// Util.title(to.meta.title);
	//加载loading的部分
	console.log(to.name)
	if (to.name == "linkOnlinehall") {
		window.open(ExternalUrl, "_blank")
	} else if (to.name == "djjk" || to.name == "tlyc" || to.name == "zhzm" || to.name == 'jcfxpt') {
		// //web端跳转页面

		const urlencode = str => {
			str = (str + "").toString()

			return encodeURIComponent(str)
				.replace(/!/g, "%21")
				.replace(/'/g, "%27")
				.replace(/\(/g, "%28")
				.replace(/\)/g, "%29")
				.replace(/\*/g, "%2A")
				.replace(/%20/g, "+")
		}
		window.open(
			`${config.ConfigBaseURL}/${this.$ServiceName}/oauth/${to.name}?token=${urlencode(window.sessionStorage.getItem("token"))}`
			// "https://www.baidu.com/?token=UGxzMnk2dDNUOWxWZzB1OVFwMysvUUZLYldQczNqWW5IM01uNWtidXFZVnV6b2NXekthUWdNVWVCM0pxajJRS3pWeTBvaW5WcnhDMmZaYnhCdXowMk1YN29lYi9XTWE1UEFUa0hKc1IxOWpMTFJOR0tRTnVjaWFjajFMdXJJR2ljenBJSHora2k5K0JWeFlyV0JGLzlVVzFUZU9sVnFROEpSeFdmWjlnMXN2TE0wdDdYTDZlNnFDdVVLeERLajBJYVJnTkcxMTBmRVUwQVVnQm84S1hXRGVmSFNXc1VReXBxV2hPeHFxNGNtNFpodFluQWY0dW9UdjR2cU14RlRTOU9qZThmc3ZWRjBoOUlXeVJyaDJhS25OZnlrcWNva3B1NDkzY1ZkMklhVytwWi9rYXNxNCtqTEszU2oyWHZuaG14MGJBY3IwYk14S29VSjVIbmF2dzN1MHBBRDNxSjI0d25wYnZBRUF4QjJYOENmeTExRjlPdDdUbEwvRi93UTlmZ1dkOFg0ZFlKVDhuNHk3RlpFRStMckJ3dVk1eTFrY3hYLzhNMSt5bjZJU2ZOZTQyYW1SL29KV2tpQWZqNGJnRmxCNWNpUVhOMENMU0NSampuWGljZTRaWU5KQ1BuNXdQZUhyYndJck5QZzJYeWVWTnBnUm5BRGJ2RzQwMmpjTXBFdHNsSWpFTWM2UGJHMDVpdmtiOWJtM0ZHbGJNVXJiekRNeE5aMCtia3o0ZElxd3BMdmxPUHhYWWwrV2ZWOEVhbkRJQUU1NjE4ZFgzMGNOL204MzBSV25Fb2R2ay9LdkQ%3D&verityUrl=aHR0cDovL3BnbS5wb3J0emouY29tL2NyYW5lLW1zLXNlcnZpY2Uvb2F1dGgvdmVyaXR5X3Rva2Vu"
		)
	} else {
		next()
	}
})

router.afterEach(to => {
	// Util.openNewPage(router.app, to.name, to.params, to.query);
	// iView.LoadingBar.finish();
	window.scrollTo(0, 0)
})
